<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";


$campaign = mysql_fetch_row(mysql_query("select * from adtrackz_campaigns where ID='$cID'"));
$months = array(1 => 'January', 'February','March','April','May','June','July','August','September','October','November','December');

?>


<table width="100%">
<tr>
	<td>
		<p class="small" align="left">Campaigns > <?=$campaign[2]?> > Click History</p>
	</td>
	<td>
		<p class="small" align="right">
			<?=$campaign[2]?> Menu<br>
			<a href="cpgn-edit.php?cID=<?=$cID?>">Edit</a> |
			<a href="dstats-summary.php?&cID=<?=$cID?>">Summary</a> | 
			<a href="dstats-history.php?view=daily&cID=<?=$cID?>">History</a><br>
			<a href="dstats-sales.php?cID=<?=$cID?>">Sales</a> | 
			<a href="dstats-actions.php?cID=<?=$cID?>">Actions</a> | 
			<a href="dstats-urls.php?cID=<?=$cID?>">Referring Urls</a><br>
			<?=$campaign[2]?> SubCampaign Menu <br>
			<a href="addsub.php?cID=<?=$cID?>">Add</a> | 
			<a href="subcpgn-edit.php?cID=<?=$cID?>">Edit</a> |
			<a href="submasscampaign.php?cID=<?=$cID?>">Manage</a>
		</p>
	</td>
</tr>
</table>




<p class="heading">
Statistic History for <?=ucfirst($campaign[2])?>
</p>

<p>View a history of visitors for this campaign. Select an option below.</p>

<p>
<a href="dstats-history.php?view=hourly&cID=<?=$cID?>">Hourly</a> | 
<a href="dstats-history.php?view=daily&cID=<?=$cID?>">Daily</a> | 
<a href="dstats-history.php?view=monthly&cID=<?=$cID?>">Monthly</a> | 
<a href="dstats-history.php?view=yearly&cID=<?=$cID?>">Yearly</a>
</p>
<?

########################################################################################
#    DAILY
########################################################################################

if ($view == "daily" || !$view)
{
	//convert time period to timestamp	
	if (!$month && !$year) //if no date selected, choose this month
	{
		//get this month
		$days_in_month = date("t");
		$from_timestamp = mktime (0,0,0,date("m"),01,date("Y")); 
		$to_timestamp = mktime (23,59,59,date("m"),$days_in_month,date("Y")); 
		$month = date("m");
		$year = date("Y");
	}
	else
	{
		$days_in_month = date("t", mktime(0, 0, 0, $month, 1, $year));
		$from_timestamp = mktime (0,0,0,$month,01,$year); 
		$to_timestamp = mktime (23,59,59,$month,$days_in_month,$year); 
	}

	$trim_month = trim(intval($month));
?>

<center>
<p><u>Daily Statistics for <?="$months[$trim_month], $year"?>
</u></p>
<form action="dstats-history.php" method="post">
<table>
<tr>
	<td>Select a month:&nbsp;</td>
	<td>
		<select name="month">	
		<option value="01">Jan</option>
		<option value="02">Feb</option>
		<option value="03">Mar</option>
		<option value="04">Apr</option>
		<option value="05">May</option>
		<option value="06">Jun</option>
		<option value="07">Jul</option>
		<option value="08">Aug</option>
		<option value="09">Sep</option>
		<option value="10">Oct</option>
		<option value="11">Nov</option>
		<option value="12">Dec</option>
		</select>
	</td>
	<td>
		<select name="year">
		<option value="2004">2004</option>
		<option value="2005">2005</option>
		<option value="2006">2006</option>
		<option value="2007">2007</option>
		<option value="2008">2008</option>
		<option value="2009">2009</option>
		<option value="2010">2010</option>
		<option value="2011">2011</option>
		<option value="2012">2012</option>
		<option value="2013">2013</option>
		<option value="2014">2014</option>
		</select>
	</td>
	<td colspan="3">
		<input type="submit" name="submit" value="View Month"  >
		<input type="hidden" name="cID" value="<?=$cID?>">
		<input type="hidden" name="view" value="daily">
	</td>
</tr>
</table>
</form>
</center>
<b>All Clicks</b><br><br>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Clicks</td>
	<td class="tbhead1">&nbsp;</td>
</tr>


<?
	#-------------------------
	# ALL CLICKS - DAILY
	#--------------------------
	//need to build an array of days and the # of hits for each day for this month
	$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID='' && timest >= '$from_timestamp' && timest <= '$to_timestamp'");

	if (!mysql_num_rows($clickdata))
	{
		echo "<tr><td colspan=3>There are no clicks to display.</td></tr>";
	}
	else
	{
		while ($click = mysql_fetch_row($clickdata))
		{
			if (date("n",$click[0]) == $trim_month) //if this click was during selected month
			{
				// +1 for clicks for this day in this month
				$day_of_month = date("j", $click[0]); 
				$dailyclicks[$day_of_month] += 1;
			}
		}
		ksort($dailyclicks);

		//set the widths for the graphs, longest bar = 350 pixels
		$max = max($dailyclicks);
		foreach($dailyclicks as $key => $value)
		{
			if ($value == $max)
				$width = 350;
			else
				$width = $value / $max * 350;
			echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
		}
		echo "</table></p>";
	}

	#-------------------------
	# SUB CLICKS - DAILY
	#--------------------------
	$uniquesubdata = mysql_query("select ID from adtrackz_subcampaigns where campaignID='$cID'");

	echo "<br><br><b>By Subcampaigns</b><br><br>";
	while ($subID = mysql_fetch_row($uniquesubdata))
	{	
		$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[0]'"));
//print sub table headers
print <<<END
&nbsp;&nbsp;<b>$sub_adcode[0]</b>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Clicks</td>
	<td class="tbhead1">&nbsp;</td>
</tr>
END;
		//need to build an array of days and the # of hits for each day for this month
		$subclickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID ='$subID[0]' && timest >= '$from_timestamp' && timest <= '$to_timestamp'");

		if (!mysql_num_rows($subclickdata))
		{
			echo "<tr><td colspan=3>There are no clicks to display.</td></tr>";
		}
		else
		{
			//create array of clicks for each day in this month
			unset ($dailysubclicks);
			while ($subclick = mysql_fetch_row($subclickdata))
			{
				if (date("n",$subclick[0]) == $trim_month) //if this click was during selected month
				{
					// +1 for clicks for this day in this month
					$day_of_month = date("j", $subclick[0]); 
					$dailysubclicks[$day_of_month] += 1;
				}
			}
			ksort($dailysubclicks);

			$max = max($dailysubclicks);
			foreach($dailysubclicks as $key => $value)
			{
				if ($value == $max)
					$width = 350;
				else
					$width = $value / $max * 350;
				echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
			}
		}
		echo "</table></p>";
	}
}

########################################################################################
#    HOURLY
########################################################################################
elseif($view == "hourly")
{
	#-------------------------
	# ALL CLICKS - HOURLY
	#--------------------------
?>
	<center><p><u>Hourly Statistics</u></p></center>

	<b>All Clicks</b><br><br>
	<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
	<tr>
		<td class="tbhead1">Date</td>
		<td class="tbhead1">Clicks</td>
		<td class="tbhead1">&nbsp;</td>
	</tr>
<?
	$hours_array = array("0:00 - 1:00", "1:00 - 2:00", "2:00 - 3:00", "3:00 - 4:00", "4:00 - 5:00", "5:00 - 6:00", "6:00 - 7:00", "7:00 - 8:00", "8:00 - 9:00", "9:00 - 10:00", "10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00", "13:00 - 14:00", "14:00 - 15:00", "15:00 - 16:00", "16:00 - 17:00", "17:00 - 18:00", "18:00 - 19:00", "19:00 - 20:00", "20:00 - 21:00", "21:00 - 22:00", "22:00 - 23:00", "23:00 - 24:00");
	$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID=''");
	if (!mysql_num_rows($clickdata))
		echo "<tr><td colspan=3>There are no clicks to display.</td></tr>";
	else
	{
		while ($click = mysql_fetch_row($clickdata))
		{
			//get hour of each click and build array
			$time = getdate($click[0]);
			$hour = $time['hours'];
			$hourlyclicks[$hour] += 1;
		}
		ksort($hourlyclicks);

		//set the widths for the graphs, longest bar = 350 pixels
		$max = max($hourlyclicks);
		foreach($hourlyclicks as $key => $value)
		{
			if ($value == $max)
				$width = 350;
			else
				$width = $value / $max * 350;
			$key_trim = trim(intval($key));
			echo "<tr><td>$hours_array[$key_trim]</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
		}
		echo "</table></p>";
	}

	#-------------------------
	# SUB CLICKS - HOURLY
	#--------------------------

	$uniquesubdata = mysql_query("select ID from adtrackz_subcampaigns where campaignID='$cID'");

	echo "<br><br><b>By Subcampaigns</b><br><br>";
	while ($subID = mysql_fetch_row($uniquesubdata))
	{		
		$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[0]'"));
//print sub table headers
print <<<END
&nbsp;&nbsp;<b>$sub_adcode[0]</b>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Clicks</td>
	<td class="tbhead1">&nbsp;</td>
</tr>
END;
		$hours_array = array("0:00 - 1:00", "1:00 - 2:00", "2:00 - 3:00", "3:00 - 4:00", "4:00 - 5:00", "5:00 - 6:00", "6:00 - 7:00", "7:00 - 8:00", "8:00 - 9:00", "9:00 - 10:00", "10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00", "13:00 - 14:00", "14:00 - 15:00", "15:00 - 16:00", "16:00 - 17:00", "17:00 - 18:00", "18:00 - 19:00", "19:00 - 20:00", "20:00 - 21:00", "21:00 - 22:00", "22:00 - 23:00", "23:00 - 24:00");
		$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID='$subID[0]'");
		unset ($hourlyclicks);
		if (!mysql_num_rows($clickdata))
		{
			echo "<tr><td colspan=3>There are no clicks to display for this month.</td></tr>";
		}
		else
		{
			while ($click = mysql_fetch_row($clickdata))
			{
				//get hour of each click and build array
				$time = getdate($click[0]);
				$hour = $time['hours'];
				$hourlyclicks[$hour] += 1;
			}
			ksort($hourlyclicks);

			//set the widths for the graphs, longest bar = 350 pixels
			$max = max($hourlyclicks);
			foreach($hourlyclicks as $key => $value)
			{
				if ($value == $max)
					$width = 350;
				else
					$width = $value / $max * 350;
				$key_trim = trim(intval($key));
				echo "<tr><td>$hours_array[$key_trim]</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
			}
		}
		echo "</table></p>";
	}
}


//WEEKLY
elseif($view == "weekly")
{
	echo "weekly";
}

########################################################################################
#    MONTHLY
########################################################################################
elseif($view == "monthly")
{
	//convert time period to timestamp
	$days_in_month = date("t");
	if (!$year)
	{
		//get this year
		$from_timestamp = mktime (0,0,0,01,01,date("Y")); 
		$to_timestamp = mktime (23,59,59,12,$days_in_month,date("Y")); 
		$year = date("Y");
	}
	else
	{
		$from_timestamp = mktime (0,0,0,01,01,$year); 
		$to_timestamp = mktime (23,59,59,12,$days_in_month,$year); 
	}

?>
	<center>
	<p><u>Monthly Statistics</u></p>
<form action="dstats-history.php" method="post">
<table>
<tr>
	<td>Select a year:&nbsp;</td>
	<td>
		<select name="year">
		<option value="2004">2004</option>
		<option value="2005">2005</option>
		<option value="2006">2006</option>
		<option value="2007">2007</option>
		<option value="2008">2008</option>
		<option value="2009">2009</option>
		<option value="2010">2010</option>
		<option value="2011">2011</option>
		<option value="2012">2012</option>
		<option value="2013">2013</option>
		<option value="2014">2014</option>
		</select>
	</td>
	<td colspan="3">
		<input type="submit" name="submit" value="View Year"  >
		<input type="hidden" name="cID" value="<?=$cID?>">
		<input type="hidden" name="view" value="monthly">
	</td>
</tr>
</table>
</form>
	</center>

	<b>All Clicks</b><br><br>
	<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
	<tr>
		<td class="tbhead1">Date</td>
		<td class="tbhead1">Clicks</td>
		<td class="tbhead1">&nbsp;</td>
	</tr>
<?

	#-------------------------
	# ALL CLICKS - MONTHLY
	#--------------------------
	//need to build an array of days and the # of hits for each day for this month
	$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID='' && timest >= '$from_timestamp' && timest <= '$to_timestamp'");

	if (!mysql_num_rows($clickdata))
	{
		echo "<tr><td colspan=3>There are no clicks to display for this year.</td></tr>";
	}
	else
	{
		while ($click = mysql_fetch_row($clickdata))
		{
			//get month 
			$time = getdate($click[0]);
			$month = $time['mon'];
			$monthlyclicks[$year."-".$month] += 1;
		}
		ksort($monthlyclicks);

		//set the widths for the graphs, longest bar = 350 pixels
		$max = max($monthlyclicks);
		foreach($monthlyclicks as $key => $value)
		{
			if ($value == $max)
				$width = 350;
			else
				$width = $value / $max * 350;
			echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
		}
		echo "</table></p>";	
	}

	#-------------------------
	# SUB CLICKS - MONTHLY
	#--------------------------
$uniquesubdata = mysql_query("select ID from adtrackz_subcampaigns where campaignID='$cID'");

	echo "<br><br><b>By Subcampaigns</b><br><br>";
	while ($subID = mysql_fetch_row($uniquesubdata))
	{		
		$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[0]'"));
//print sub table headers
print <<<END
&nbsp;&nbsp;<b>$sub_adcode[0]</b>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Clicks</td>
	<td class="tbhead1">&nbsp;</td>
</tr>
END;
		$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID='$subID[0]' && timest >= '$from_timestamp' && timest <= '$to_timestamp'");
		unset ($monthlyclicks);
		if (!mysql_num_rows($clickdata))
		{
			echo "<tr><td colspan=3>There are no clicks to display for this month.</td></tr>";
		}
		else
		{
			while ($click = mysql_fetch_row($clickdata))
			{
				//get month 
				$time = getdate($click[0]);
				$month = $time['mon'];
				$monthlyclicks[$year."-".$month] += 1;
			}
			ksort($monthlyclicks);

			//set the widths for the graphs, longest bar = 350 pixels
			$max = max($monthlyclicks);
			foreach($monthlyclicks as $key => $value)
			{
				if ($value == $max)
					$width = 350;
				else
					$width = $value / $max * 350;
				$key_trim = trim(intval($key));
				echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
			}
		}
		echo "</table></p>";
	}
}
########################################################################################
#    YEARLY
########################################################################################
elseif($view == "yearly")
{
?>
	<center><p><u>Yearly Statistics</u></p></center>

	<b>All Clicks</b><br><br>
	<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
	<tr>
		<td class="tbhead1">Date</td>
		<td class="tbhead1">Clicks</td>
		<td class="tbhead1">&nbsp;</td>
	</tr>
<?
	#-------------------------
	# ALL CLICKS - YEARLY
	#--------------------------
	$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID=''");
	if (!mysql_num_rows($clickdata))
		echo "<tr><td colspan=3>There are no clicks to display.</td></tr>";
	else
	{
		while ($click = mysql_fetch_row($clickdata))
		{
			//get month 
			$time = getdate($click[0]);
			$clickyear = $time['year'];
			$yearlyclicks[$clickyear] += 1;
		}
		ksort($yearlyclicks);

		//set the widths for the graphs, longest bar = 350 pixels
		$max = max($yearlyclicks);
		foreach($yearlyclicks as $key => $value)
		{
			if ($value == $max)
				$width = 350;
			else
				$width = $value / $max * 350;
			echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
		}
		echo "</table></p>";	
	}

	#-------------------------
	# SUB CLICKS - YEARLY
	#--------------------------
	
	$uniquesubdata = mysql_query("select ID from adtrackz_subcampaigns where campaignID='$cID'");

	echo "<br><br><b>By Subcampaigns</b><br><br>";
	while ($subID = mysql_fetch_row($uniquesubdata))
	{		
		$sub_adcode = mysql_fetch_row(mysql_query("select adcode from adtrackz_subcampaigns where ID='$subID[0]'"));
//print sub table headers
print <<<END
&nbsp;&nbsp;<b>$sub_adcode[0]</b>
<table width="580" border="1" cellspacing="0" cellpadding="5" align="center" bordercolor="#e0e0e0">
<tr>
	<td class="tbhead1">Date</td>
	<td class="tbhead1">Clicks</td>
	<td class="tbhead1">&nbsp;</td>
</tr>
END;
		$clickdata = mysql_query("select timest from adtrackz_clicks where campaignID='$cID' && subID='$subID[0]' ");
		unset ($yearlyclicks);
		if (!mysql_num_rows($clickdata))
		{
			echo "<tr><td colspan=3>There are no clicks to display for this month.</td></tr>";
		}
		else
		{
			while ($click = mysql_fetch_row($clickdata))
			{
				//get month 
				$time = getdate($click[0]);
				$clickyear = $time['year'];
				$yearlyclicks[$clickyear] += 1;
			}
			ksort($yearlyclicks);

			//set the widths for the graphs, longest bar = 350 pixels
			$max = max($yearlyclicks);
			foreach($yearlyclicks as $key => $value)
			{
				if ($value == $max)
					$width = 350;
				else
					$width = $value / $max * 350;
				$key_trim = trim(intval($key));
				echo "<tr><td>$key</td><td class=tb2>$value</td><td><img src=bar.gif height=10 width=$width></td></tr>";
			}
		}
		echo "</table></p>";
	}


}

require "footer.php";
?>
